package com.momoauth.api_client.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.momoauth.api_client.entity.User;
import com.momoauth.api_client.entity.User_Group;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserGroupMapper extends BaseMapper<User_Group> {

    /**
     * 查询是否存在用户使用该群组
     * @param group_id
     * @return
     */
    @Select("SELECT 1 FROM user_group WHERE group_id = #{group_id} LIMIT 1")
    Integer existUseGroup(Long group_id);

    /**
     * 使用该群组的用户列表
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT id,userId,username,realName,createTime " +
            "FROM user " +
            "INNER JOIN user_group ON user_id = userId " +
            "${ew.customSqlSegment}")
    List<User> getUserListFromGroup(Page<User> page, @Param(Constants.WRAPPER) QueryWrapper wrapper);

    /**
     * 判断是否重复加入群组
     * @param user_group
     * @return
     */
    @Select("SELECT 1 FROM user_group WHERE user_id = #{user_id} AND group_id = #{group_id} LIMIT 1")
    Integer ifRepeatJoinGroup(User_Group user_group);
}
